package com.ycxh.controller;

import com.ycxh.module.mybatis.TestTable01;
import com.ycxh.module.mybatis.TestTable01Dao;
import com.ycxh.module.mybatis.batch.BatchUtil_bak;
import com.ycxh.plugin.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ycxh.module.mybatis.batch.BatchUtil.batchHandle;

@RestController
@RequestMapping("/mybatis")
public class MybatisController {

    @Autowired
    private TestTable01Dao dao;

    // 测试自定义mybatis分页插件 http://127.0.0.1:8088/deploy/mybatis/selectList
    @GetMapping("selectList")
    public void selectList() throws Exception {
        PageParam pageParam = new PageParam(1, 20);
        List<TestTable01> testTable01s = dao.selectList(pageParam);
        System.out.println(testTable01s);
    }

    // 测试批量操作 http://127.0.0.1:8088/deploy/mybatis/batchTest
    @GetMapping("batchTest")
    public void batchTest() throws Exception {
        List<TestTable01> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            TestTable01 testTable01 = new TestTable01();
            testTable01.setId(i + 300);
            testTable01.setName(i + 300 + "name");
            testTable01.setAge(i + 300);
            testTable01.setSex(i % 2 + 1);
            testTable01.setCreatedTime(new Date());

            list.add(testTable01);
        }

        /**
         * select * from test_table_01 where id >= 300
         */
        dao.batchDelete(list);

        // dao.batchInsert(list);
        // 分批处理 单个参数
        batchHandle(list, t1 -> dao.batchInsert(t1));

        // dao.batchUpdateSex(list, 2);
        // 分批处理 多个参数
        // BatchUtil_bak.batchHandle((t1, t2) -> dao.batchUpdateSex(t1, (Integer)t2[0]), list, 2);
        // 简化 一开始想复杂了 只有t1是变化的，所以用形参 其他都是不用变化的 所以它本质其实也是一个参数
        batchHandle(list, t1 -> dao.batchUpdateSex(t1, 2));

        // dao.batchUpdate(list);

        // dao.batchDeleteByNameAndSex(list);

        System.out.println();

    }


}
